@echo off
chcp 65001 > nul
echo ========================================
echo 启动知识库服务并开始扫描
echo ========================================
echo.

REM 设置环境变量
set OH_CODE_PATH=%CD%\OHCode
set GLM_API_KEY=df6af53dd93b4afe9c1f0781f198305a.zcW6GYuBCOz3Xb27

echo 🔧 环境配置:
echo    OH_CODE_PATH = %OH_CODE_PATH%
echo.

REM 检查OHCode目录
if not exist "%OH_CODE_PATH%" (
    echo ⚠️  警告: OHCode目录不存在: %OH_CODE_PATH%
    echo    知识库将无法扫描代码
    echo.
    set /p CONTINUE="是否继续？(Y/N): "
    if /i not "%CONTINUE%"=="Y" exit /b 1
)

echo ========================================
echo 【步骤1】编译并启动知识库服务
echo ========================================
echo.

cd openharmony-knowledge-service

echo 📦 编译项目...
call mvn clean package -DskipTests
if errorlevel 1 (
    echo ❌ 编译失败！
    pause
    exit /b 1
)

echo.
echo ✅ 编译成功！
echo.
echo 🚀 启动服务...
echo    (服务将在新窗口启动)
echo.

start "Knowledge Service" cmd /k "java -Xms512m -Xmx2048m -jar target\openharmony-knowledge-service-1.0.0.jar --spring.profiles.active=prod"

cd ..

echo.
echo ⏳ 等待服务启动（30秒）...
timeout /t 30 /nobreak > nul

echo.
echo ========================================
echo 【步骤2】检查服务状态
echo ========================================
echo.

curl -s http://localhost:8087/health > nul 2>&1
if %errorlevel% neq 0 (
    echo ⚠️  服务可能还在启动中，请稍等...
    echo    继续尝试启动扫描...
    timeout /t 20 /nobreak > nul
) else (
    echo ✅ 知识库服务已启动！
)

echo.
echo ========================================
echo 【步骤3】启动代码扫描
echo ========================================
echo.

echo 📊 查看当前统计...
curl -s http://localhost:8087/api/knowledge/stats/summary
echo.
echo.

echo 🚀 开始扫描 OpenHarmony 代码...
echo    扫描路径: %OH_CODE_PATH%
echo.
echo ⏳ 扫描中，这可能需要较长时间...
echo    (取决于代码库大小)
echo.

curl -X POST http://localhost:8087/api/knowledge/scan/start ^
     -H "Content-Type: application/json" ^
     -d "{\"force\": false}"

echo.
echo.
echo ========================================
echo ✓ 扫描任务已提交！
echo ========================================
echo.
echo 📝 后续操作:
echo    1. 查看扫描进度:
echo       curl http://localhost:8087/api/knowledge/scan/status
echo.
echo    2. 查看知识库统计:
echo       curl http://localhost:8087/api/knowledge/stats/summary
echo.
echo    3. 在浏览器中访问:
echo       http://localhost:8087/h2-console
echo       (查看数据库，JDBC URL: jdbc:h2:file:./openharmony-knowledge-service/data/knowledge)
echo.
echo 💡 提示:
echo    - 扫描是后台任务，可以继续使用其他功能
echo    - 扫描完成后知识库会自动可用
echo    - 可以随时查看扫描进度
echo.

pause

